package Day2025_03_16;

import java.util.HashMap;
import java.util.Map;

public class LeetCode1218 {
//    public int longestSubsequence(int[] arr, int d) {
//        int n = arr.length;
//        int[] dp = new int[n];
//        for (int i = 0; i < n; i++) arr[i] = 1;
//        int ret = 1;
//        for (int i = 1; i < n; i++) {
//            for (int j = i - 1; j >= 0; j--) {
//                if (arr[i] - arr[j] == d) dp[i] = Math.max(dp[i], dp[j] + 1);
//            }
//            ret = Math.max(ret, dp[i]);
//        }
//        return ret;
//    }

    public int longestSubsequence(int[] arr, int d) {
        Map<Integer, Integer> hash = new HashMap<>();
        int ret = 1;
        for (int x : arr) {
            hash.put(x, hash.getOrDefault(x - d, 0) + 1);
            ret = Math.max(ret, hash.get(x));
        }
        return ret;
    }

    public static void main(String[] args) {
        LeetCode1218 leetCode1218 = new LeetCode1218();
        System.out.println(leetCode1218.longestSubsequence(new int[]{1, 2, 3, 4},1));
    }
}
